package com.deer.wms.system.manage.web;

import com.deer.wms.project.root.util.RedisUtil;
import com.deer.wms.system.manage.constant.SysParamConstant;
import com.deer.wms.system.manage.model.*;
import com.deer.wms.system.manage.model.SysParamInsert;
import com.deer.wms.system.manage.service.SysParamDetailService;
import com.deer.wms.system.manage.service.SysParamService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;

import java.util.List;

/**
* Created by guo on 2019/07/12.
*/
@Api(description = "系统参数信息api接口")
@RestController
@RequestMapping("/sys/params")
public class SysParamController {

    @Autowired
    private SysParamService sysParamService;

    @Autowired
    private SysParamDetailService sysParamDetailService;

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("/getByCode")
    public Result getByCode(String code) {
        SysParam sysParam = sysParamService.findBy("paramCode",code);
        return ResultGenerator.genSuccessResult(sysParam.getValue());

    }

    @PostMapping("/save")
    public Result add(@RequestBody SysParam sysParam) {
        sysParamService.createSysParam(sysParam);
        removeRedisA();
        removeRedisB();
        removeRedisC();
        sysParamService.reSetRedis();
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/delete")
    public Result delete( Integer id) {
        sysParamService.removeById(id);
        sysParamService.reSetRedis();
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody SysParamInsert sysParamInsert) {
        SysParam sysParam = sysParamInsert;
        sysParamService.updateById(sysParam);
        sysParamDetailService.deleteBySysParamId(sysParam.getSysParamId());
        List<SysParamDetail> sysParamDetails = sysParamInsert.getDetails();
        if(sysParamDetails != null){
            for (SysParamDetail detail: sysParamDetails ) {
                sysParamDetailService.save(detail);
            }

        }
        removeRedisA();
        removeRedisB();
        removeRedisC();
        sysParamService.reSetRedis();
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/updateAllValue")
    public Result updateAllValue(@RequestBody SysParamAllValue sysParamAllValue) {
        List<SysParam> list = sysParamAllValue.getSysParams();
        if(list != null){
            for (SysParam sysParam: list ) {
                sysParamService.updateById(sysParam);
            }
        }
        sysParamService.reSetRedis();
        return ResultGenerator.genSuccessResult();
    }



    @GetMapping
    public Result list(SysParamCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<SysParamDto> list = sysParamService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    private void removeRedisA() {
        List<SysParam> list = sysParamService.list();
        for (SysParam sysParam : list) {
            redisUtil.remove(sysParam.getMemo() + "SysParam");
        }
    }
    private void removeRedisB() {
        SysParamCriteria criteria = new SysParamCriteria();
        List<SysParamDto> list = sysParamService.findList(criteria);
        for (SysParam sysParam : list) {
            redisUtil.remove(sysParam.getParamCode()+"SysParam");
        }
    }
    private void removeRedisC() {
        SysParamCriteria criteria = new SysParamCriteria();
        List<SysParamDto> list = sysParamService.findList(criteria);
        for (SysParam sysParam : list) {
            redisUtil.remove(sysParam.getModuleCode()+"SysParam");
        }
   }


    @GetMapping("/findByMemo")
    public Result list(String  memo) {
        List<SysParam> list = (List<SysParam>) redisUtil.get(memo+"SysParam");
        if(list==null || list.size()==0){
            list = sysParamService.findByMemo(memo);
            redisUtil.set(memo+"SysParam",list);
        }


        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/findByModuleCode")
    public Result findByModuleCode(String  moduleCode) {


        List<SysParamDto> list = (List<SysParamDto>) redisUtil.get(moduleCode+"SysParam");
        if(list==null || list.size()==0){
            SysParamCriteria criteria = new SysParamCriteria();
            criteria.setModuleCode(moduleCode);
            list = sysParamService.findList(criteria);
            redisUtil.set(moduleCode+"SysParam",list);
        }


        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/findByParamCode")
    public Result findByParamCode(String  paramCode) {

        Object object = redisUtil.get(paramCode+"SysParam");
        List<SysParamDto> list = (List<SysParamDto>) redisUtil.get(paramCode+"SysParam");
        if(list==null || list.size()==0){
            SysParamCriteria criteria = new SysParamCriteria();
            criteria.setParamCode(paramCode);
            list = sysParamService.findList(criteria);
            redisUtil.set(paramCode+"SysParam",list);
        }


        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }



    @GetMapping("/detailList")
    public Result detailList() {
        Object object = redisUtil.get("sysParamDtos");
        List<SysParamDto> list = (List<SysParamDto>)object;
        if(list==null || list.size() == 0){
            list =sysParamService.reSetRedis();
        }
        String aaa= SysParamConstant.paramValues.get("13");
        return ResultGenerator.genSuccessResult(list);
    }



}
